//This code requires the Minim sound library available here: http://code.compartmental.net/tools/minim/
//Sound effects were obtained from www.freesound.org

import ddf.minim.*; //The minim sound library

Trial trial;

int num_trials;      //the current trial number
int max_trials = 10;

//Intuitively, these should be properties of the Trial objects
//but because we don't want to waste time loading sound effects
//at the beginning of each trial, we can conserve CPU time by
//loading them once when the program starts and then just
//playing them from within Trial objects
Minim minim;
AudioSample sound_effect_correct;
AudioSample sound_effect_incorrect;
AudioSample sound_effect_stimulus_presentation;

void setup() {
  // Sets up the program
  
  size(800,600);
  background(0);

  trial = createRandomTrial();
  minim = new Minim(this);
  
  sound_effect_correct = minim.loadSample("se_correct.wav",2048);
  sound_effect_incorrect = minim.loadSample("se_incorrect.wav",2048);
  sound_effect_stimulus_presentation = minim.loadSample("se_stimulus_presentation2.wav",2048);
  num_trials = 1;
}


void mousePressed() {
  //Detect mouse clicks and passes them on to the current trial
  
  trial.click();
}

Trial createRandomTrial() {
  //This creates a random trial. Useful for testing purposes but we'll probably have
  //to replace this with something that changes trials systematically according to
  //the current condition
  
  int sample_num = int(random(1,10));
  int comparison_num = int(random(1,10));
  int uncertainty_r = int(random(0,2));
  while (comparison_num == sample_num) {
    comparison_num = int(random(1,10));
  }
  return new Trial(sample_num,sample_num,comparison_num,uncertainty_r);
}


void draw() {
  //This is our main loop
  
 if (num_trials < max_trials) {
   if (trial.finished == false) trial.drawTrial();
   else {
     num_trials++;
     trial = createRandomTrial();
   }
 }
}

void stop() {
  //This function is called automatically when the program stops
  //Make sure we stop all of our sound effects and close minim
  
  if (sound_effect_correct != null) sound_effect_correct.close();
  if (sound_effect_incorrect != null) sound_effect_incorrect.close();
  if (sound_effect_stimulus_presentation != null) sound_effect_stimulus_presentation.close();
  minim.stop();
  super.stop();
}
